package com.skillworld.webapp.model.roomdservice;

import java.util.HashMap;
import java.util.Map;

import javax.annotation.PostConstruct;

import org.springframework.stereotype.Service;


@Service("RoomdService")
public class RoomdServiceImpl implements RoomdService
{    
	private Map<String, Roomd> servers = new HashMap<String, Roomd>();
	
	
	@PostConstruct
	public void  initialize()
	{
		load();
	}
	
	private void load()
	{
	    registerServer(RoomdConfig.getRoomd("WORLD"));
        registerServer(RoomdConfig.getRoomd("DEPCOOK"));
        registerServer(RoomdConfig.getRoomd("DEPTRAVEL"));
        registerServer(RoomdConfig.getRoomd("DEPFUN"));
        registerServer(RoomdConfig.getRoomd("DEPSPORT"));
        registerServer(RoomdConfig.getRoomd("DEPHOME"));
        registerServer(RoomdConfig.getRoomd("DEPHEALTH"));
	}
	
/*	private void populate()
	{		
		registerServer(new Roomd("WORLD", "skillworld.no-ip.org", 4001, 4000));
		registerServer(new Roomd("DEPCOOK", "skillworld.no-ip.org", 4002, 4000));
		registerServer(new Roomd("DEPTRAVEL", "skillworld.no-ip.org", 4003, 4000));
		registerServer(new Roomd("DEPFUN", "skillworld.no-ip.org", 4004, 4000));
		registerServer(new Roomd("DEPSPORT", "skillworld.no-ip.org", 4005, 4000));
		registerServer(new Roomd("DEPHOME", "skillworld.no-ip.org", 4006, 4000));
		registerServer(new Roomd("DEPHEALTH", "skillworld.no-ip.org", 4007, 4000));
	}*/
	
	private void registerServer(Roomd server)
    {
        servers.put(server.getMap(), server);
    }
	

	public Roomd findServer(String mapName)
	{
	    return servers.get(mapName);
	}

	
/*	private void remEnd(String ip, int port, String mapa)
	{
		Roomd e = null;
		AdressList le;
		//
		
		e = new Roomd(mapa, ip, port);
		le = this.lstServers.get(mapa);
		if(le!=null)
		{ le.removeEnd(e.toString()); }
	}
	private void remEnd(Roomd e)
	{
		AdressList le;
		//
		le = this.lstServers.get(e.getMap());
		if(le!=null)
		{ le.removeEnd(e.toString()); }
	}
	
	public Roomd getFlashPolicyAddr() {
	    return this.flashPolicy.clone();
	}
	
	
	public Roomd getStringCon(String nomeMapa)
	{
		Roomd ipc = null;
		AdressList le = null;
		//
		
		le = this.lstServers.get(nomeMapa);
		if(le!=null)
		{
			ipc = le.getRandom();
		}
		return ipc;
	}
	

	public ArrayList<Roomd> getAllIPs()
	{
		ArrayList<Roomd> list = new ArrayList<Roomd>();
		
				
		for(AdressList le : this.lstServers.values())
		{
			list.addAll(le.getAll());
		}
			
		return list;
	}

	//@Lock(WRITE)
	public void registaServerMap(String ip, int port, String mapa)
	{ registerServer(mapa, ip, port); }
	
	//@Lock(WRITE)
	public void removeServerMap(String ip, int port, String mapa)
	{ remEnd(ip, port, mapa); }
	
	//@Schedule(second="* /60", minute="*",hour="*", persistent=false)
	public void pingSocksServers()
	{
		boolean resp = false;
		//
		
		for(Roomd e : this.getAllIPs())
		{
			resp = this.doPing(e);
			if(resp!=true)
			{ this.remEnd(e); }
		}
		System.err.println("METER OS PINGs");		
	}
	
	public boolean doPing(Roomd e)
	{
		// Falta-me código
		return false;
	}*/
	
}

